<template>
  <div class="contenBox" >
    <div class="list">
      <h3 style="color: #3c98d9; margin-top: 20px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;管理列表</h3>
      <div class="search">
        <div class='searc'>
        <el-form :inline="true" >
          <!-- 搜索区域 -->
          <el-form-item>
            <el-input
              v-model="input"
              style="width: 300px; margin-top:20px;"
              placeholder="请输入教室名称"
              
            >
              <el-button slot="append" icon="el-icon-search" @click='changeSearch'></el-button>
            </el-input>
          </el-form-item>
          <!-- 添加用户 -->
        </el-form>
        <el-button
          type="primary"
          @click="addmajor"
          style="
            height: 40px;
            background: #5ca5d2;
            border: none;
            margin-top: 20px;
          "
        >
          <i class="el-icon-plus"></i>
          新增</el-button
        >
        <el-button
          type="danger"
          @click="moreDelete"
          style="
            height: 40px;
            background: #f1818d;
            border: none;
            margin-top: 20px;
          "
        >
          <i class="el-icon-close"></i>
          批量删除</el-button
        >
        </div>
      </div>
    </div>
    <div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 94%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" type="index" width="120">
        </el-table-column>
        <el-table-column prop="classroomName" label="教室名称" itemId="classroomId" width="120">
        </el-table-column>
        <el-table-column prop="classroomAddress" label="教室地点"> </el-table-column>
        <el-table-column prop="classroomType" label="教室类型"> </el-table-column>
        <el-table-column prop="capacity" label="容纳人数"> </el-table-column>
        <el-table-column prop="status" label="启用状态">
          <template slot-scope="scope">
            <el-switch
                v-model="scope.row.status"
                active-color="#13ce66"
                inactive-color="#C2C2C2"
                active-value="0"
                inactive-value="1"
                @change="updateIsUsing(scope.row)"
              >
              </el-switch>
            </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type='text' @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                  <el-button type='text' @click="del(scope.row)">删除</el-button>  
            <!-- <span class='icon iconfont icon-bianji edit' @click="handleEdit(scope.$index, scope.row)"></span>
             &nbsp; <span class='icon iconfont icon-shanchu del' @click="del(scope.row)"></span> -->
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
          <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.pageNum"
        :page-sizes="[2, 3, 4, 5]"
        :page-size="query.pageSize"
        prev-text="上一页"
        next-text="下一页"
        layout=" prev, pager, next"
        :total="query.total"
      ></el-pagination>

          <div class="jumper">
            跳到第
            <input type="text" ref="pageNum" :value="query.pageNum" />
            页
          </div>
          <button @click="goPage">确定</button>
        </div>
    <!-- 添加模态框 -->
    <el-dialog
      title="添加教室"
      :visible.sync="dialogFormVisible"
      width="700px"
      height="500px"
    >
      <div class="addInput">
        <p class="collegeName">教室名称：</p>
        <el-input v-model="form.classroomName" placeholder="请输入教室名称"></el-input>
      </div>
      <br />
      <div class="addInput">
        <p class="collegeName">教室地点：</p>
        <el-input v-model="form.classroomAddress" placeholder="请输入教室地点"></el-input>
      </div>
      <br />
      <div class="addInput">
        <p class="collegeName">教室类型：</p>
        <el-select v-model="form.classroomType" placeholder="请选选择类型">
            <el-option
              v-for="(item, index) in typelist"
              :key="index"
              :label="item.typeName"
              :value="item.typeName"
            >
            </el-option>
          </el-select>
      </div>
      <br />
      <div class="addInput">
        <p class="collegeName">容纳人数：</p>
        <el-input v-model="form.capacity" placeholder="请输入可容纳人数"></el-input>
      </div>
      <br />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addBtn">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 修改弹框 -->
    <el-dialog
      title="修改教室"
      :visible.sync="editFlag"
      width="700px"
      height="500px"
    >
     <div class="addInput">
        <p class="collegeName">教师名称：</p>
        <el-input v-model="editform.classroomName" placeholder="请输入内容"></el-input>
      </div>
      <br />
      <div class="addInput">
        <p class="collegeName">教室地点：</p>
        <el-input v-model="editform.classroomAddress" placeholder="请输入内容"></el-input>
      </div>
      <br />
      <div class="addInput">
        <p class="collegeName">教室类型：</p>
        <el-select v-model="editform.classroomType" placeholder="请选选择类型">
            <el-option
              v-for="(item, index) in typelist"
              :key="index"
              :label="item.typeName"
              :value="item.typeName"
            >
            </el-option>
          </el-select>
      </div>
      <br />
      <div class="addInput">
        <p class="collegeName">容纳人数：</p>
        <el-input v-model="editform.capacity" placeholder="请输入内容"></el-input>
      </div>
      <br />
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFlag = false">取 消</el-button>
        <el-button type="primary" @click="editBtn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  classroomListApi,
  classroomAddApi,
  classroomDelApi,
  classroomEditApi,
  getNationApi
} from "@/api/basics/classroom";
export default {
  data() {
    return {
      tableData: [],
      query: {
        pageNum: 1,
        pageSize: 6,
        total: 0
      },
      dialogFormVisible: false,
      input: "",
      editFlag: false,
      //模态框数据
      typelist:'',
      form: {
        classroomName: "",
        classroomAddress: "",
        classroomType: "",
        capacity: "",
        status: 1,
      },
      editform: {
        classroomId: "",
        classroomName: "",
        classroomAddress: "",
        classroomType: "",
        capacity: "",
        status: 1,
      }
    };
  },
  mounted() {
    this.classroomList();
    this.getList()
  },
  methods: {
    async getList(){
     let res=await getNationApi({parentId:66}) 
     console.log('233',res)
     this.typelist=res.rows
     console.log('23',this.typelist)
   },
    classroomList() {
      classroomListApi(this.query)
        .then(res => {
          console.log(res);
          this.tableData = res.rows;
          this.query.total = res.total;
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      var arrTostring = [];
      console.log(val)
      val.forEach(item => {
        if (arrTostring.indexOf(item.id) === -1) {
          arrTostring.push(item.classroomId);
        }
      });
      this.ids = arrTostring.toString();
    },
    // 分页
    // 改变每页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pageSize = val;
      this.classroomList();
    },
    //切换页数，执行方法
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.query.pageNum = val;
      this.classroomList();
    },
    goPage() {
      let { value } = this.$refs.pageNum;
      console.log("1", value);
      this.query.pageNum = Number(value);
      this.classroomList();
    },
    //添加
    addmajor() {
      this.dialogFormVisible = true;
      this.form.classroomName = "";
      this.form.classroomAddress = "";
      this.form.classroomType = "";
      this.form.capacity = "";
    },
    addBtn() {
      if (
        this.form.classroomName == "" ||
        this.form.classroomAddress == "" ||
        this.form.classroomType == "" ||
        this.form.capacity == ""
      ) {
        this.$message.error("请完善输入内容");
      } else {
        classroomAddApi(this.form)
          .then(res => {
            console.log(res);
            this.classroomList();
            this.$message.success("添加成功");
            this.dialogFormVisible = !this.dialogFormVisible;
          })
          .catch(() => {
          });
      }
    },
    // 搜索
    changeSearch() {
      classroomListApi({ classroomName: this.input }).then(res => {
        console.log(res);
        this.tableData = res.rows;
      });
    },
    //批量删除 
    moreDelete() {
      console.log(this.ids);
      this.$confirm("确认删除记录吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          classroomDelApi({ ids: this.ids }).then(res => {
            console.log(res);
            this.$message.success("删除成功");
            this.classroomList();
          });
        })
        .catch(() => {});
    },
    //删除
    del(row) {
      this.$confirm("确认删除记录吗?", "提示", {
        type: "warning"
      })
        .then(() => {
          classroomDelApi({ ids: JSON.stringify(row.classroomId) }).then(
            res => {
              console.log(res);
              this.$message.success("删除成功");
              this.classroomList();
            }
          );
        })
        .catch(() => {});
    },
    //修改数据回调
    handleEdit(index, row) {
      console.log(row)
      this.editFlag = true;
      this.editform.classroomName = row.classroomName;
      this.editform.status = row.status;
      this.editform.classroomAddress = row.classroomAddress;
      this.editform.capacity = row.capacity;
      this.editform.classroomType = row.classroomType;
      this.editform.classroomId = row.classroomId;
    },
    //修改功能
    editBtn() {
      this.editFlag = false;
      let data = {
        classroomId: this.editform.classroomId,
        classroomName: this.editform.classroomName,
        // status: this.editform.status,
        classroomType: this.editform.classroomType,
        classroomAddress: this.editform.classroomAddress,
        capacity: this.editform.capacity
      };
      classroomEditApi(data).then(res => {
        console.log(res);
        this.$message.success("修改成功");
        this.classroomList();
      });
    },
    //修改状态
     updateIsUsing(row) {
     classroomEditApi({status:row.status,classroomId:row.classroomId})
     this.$message.success("修改成功");
    }
  }
};
</script>

<style lang="scss" scoped>
.contenBox {
  width: 104%;
  height: 100%;
  background-color: #f3f2f7;
  box-sizing: border-box;
}
.list {
  width: 94%;
  display: flex;
  background-color: #ffffff;
  justify-content: space-between;
}
.search {
  line-height: 90px;
  margin-left: 10px;
  display: flex;
  .searc{
    display: flex;
    margin: 0px 50px;
  }
}
.el-icon-edit-outline {
  color: #94ccea;
  font-size: 20px;
  font-weight: bold;
}
.el-icon-delete {
  color: #f75572;
  font-size: 20px;
  font-weight: bold;
  margin-left: 15px;
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: right;
  margin-top: 20px;
  margin-right: 150px;

  .jumper {
    font-size: 14px;
    margin-left: 20px;
    margin-right: 10px;
    display: flex;
    align-items: center;

    input {
      width: 40px;
      height: 26px;
      margin: 0 5px;
      text-align: center;
      border: solid 1px #e6e6e6;
      border-radius: 5px;
      color: #666;
      outline-style: none;
    }
  }
  button {
    height: 28px;
    line-height: 28px;
    border-radius: 5px;
    background-color: #4895db;
    border: solid 1px #4895db;
    color: #fff;
    font-size: 14px;
    padding: 0 8px;
  }
}
::v-deep .el-dialog {
  width: 700px;
  height: 500px;
  border-radius: 10px;
  font-weight: 600;
  .fade-enter-active,
  .fade-leave-active {
    transition: opacity 0.3s;
  }
  .message {
    position: absolute;
    left: 40%;
    top: 50%;
    width: 140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    color: #fff;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, 0.4);
  }
  .fade-enter, 
            .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
    opacity: 0;
  }
  .el-dialog__header {
    border-radius: 10px 10px 0 0;
    text-align: center;
    background-color: rgb(72, 149, 219);
    .el-dialog__title {
      color: #fff;
    }
    .el-dialog__headerbtn .el-dialog__close {
      color: #fff;
    }
  }
  .addInput {
    display: flex;
    justify-content: center;
    align-items: center;
    .collegeName {
      width: 70px;
    }
    .el-input {
      width: 300px;
    }
  }
  .addSelect {
    display: flex;
    justify-content: center;
    align-items: center;
    // justify-content: space-around;

    .superiorCollege {
      width: 70px;
    }
    .el-select {
      width: 300px;
    }
  }
  .el-dialog__body {
    height: 240px;
  }
  .el-dialog__footer {
    text-align: center;
    button {
      width: 100px;
      height: 35px;
      border-radius: 16px;
      line-height: 0;
    }
    .cancel {
      border: none;
      color: #848383;
      background-color: #f0f0f0;
    }
    .determine {
      border: none;
      background-color: #4995dc;
    }
  }
}
</style>

